<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/18 17:19
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */


class Cashout extends ADM_Controller
{

    const PAGE_SIZE = 20;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 提现
     */
    public function index()
    {
        $this->load->view('admin/money/cashout_list.html');
    }

    /**
     * 获取取现申请列表
     */
    public function getCashoutLists()
    {
        $types = ['all', 'pending', 'pass', 'reject', 'failed'];
        $send['state'] = false;
        do {

            $page = (int) $this->input->get('page');
            $status = $this->input->get('status');
            if (!in_array($status, $types)) {
                $send['error'] = '参数错误';
                break;
            }
            $page = !empty($page) && intval($page) > 1 ? intval($page) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $this->load->model('common/Cash_Model', 'cash');
            $results = $this->cash->getCashoutLists($status, $offset, self::PAGE_SIZE);
            if ($results) {
                $send['state'] = true;
                $send['data'] = [
                    'total' => $results['total'],
                    'nextPage' => $page * self::PAGE_SIZE < $results['total'],
                    'data' => $results['data']
                ];
            } else {
                $send['error'] = '获取取现数据';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 审核通过
     */
    public function pass()
    {
        $send['state'] = false;
        do {

            $id = $this->input->post('id');
            if (empty($id)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Cash_Model', 'cash');
            if ($ret = $this->cash->passCashout($id, true)) {
                $send['state'] = true;
                $send['data'] = $ret;
            } else {
                $error = $this->cash->getCashError();
                $send['error'] = !empty($error) ? $error : '转账失败';
            }

        } while(false);
        $this->output->send($send);
    }

    public function reject()
    {
        $send['state'] = false;
        do {

            $id = $this->input->post('id');
            $remark = $this->input->post('remark');
            if (empty($id)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Cash_Model', 'cash');
            if ($ret = $this->cash->rejectCashout($id, $remark, true)) {

                $send['state'] = true;
                $send['data'] = $ret;
            } else {
                $error = $this->cash->getCashError();
                $send['error'] = !empty($error) ? $error : '驳回失败';
            }

        } while(false);
        $this->output->send($send);
    }
}